import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/login/index.vue'
import Layout from '@/components/Layout.vue'
import NotFound from '@/components/NotFound.vue'
// isShow属性:el-menu树要不要渲染
// isMenuItem:el-menu树有没有字节点
Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/home',
      isShow: true,
      authName: '首页',
      isMenuItem: true,
      component: Layout,
      roleCode: 'home',
      children: [
        {
          id: 110,
          authName: '项目权限介绍',
          path: '/home',
          component: (resolve) => require(['@/views/home'], resolve)
        }
      ]
    },
    {
      path: '/login',
      component: Login,
      isShow: false
    },
    {
      id: 125,
      roleCode: 'user',
      authName: '用户管理',
      path: '/User',
      name: 'User',
      redirect: '/Userlist',
      icon: 'icon-user',
      isMenuItem: false,
      isShow: true,
      component: Layout,
      children: [
        {
          id: 110,
          isMenuItem: true,
          roleCode: 'userlist',
          authName: '用户列表',
          path: '/Userlist',
          component: (resolve) => require(['@/views/user/userlist.vue'], resolve)
        },
        {
          id: 120,
          isMenuItem: true,
          roleCode: 'usergrade',
          authName: '用户等级',
          path: '/Usergrade',
          component: (resolve) => require(['@/views/user/usergrade.vue'], resolve)
        }
      ]
    },
    {
      isShow: true,
      isMenuItem: false,
      id: 103,
      path: '/Roles',
      name: 'Roles',
      roleCode: 'roles',
      authName: '角色管理',
      redirect: '/Roleslist',
      icon: 'icon-tijikongjian',
      component: Layout,
      children: [
        {
          id: 111,
          isShow: true,
          isMenuItem: true,
          roleCode: 'roleslist',
          authName: '角色列表',
          path: '/Roleslist',
          component: (resolve) => require(['@/views/role/rolelist.vue'], resolve)
        }
      ]
    },
    {
      isShow: false,
      path: '*',
      component: NotFound
    }
  ]
})
// 筛选有权限的菜单
// function filterRoute(arr, arr1) {
//   console.log(arr, arr1, 'arrarr1')
//   const realRoutes = []
//   arr.forEach((v) => {
//     if (!v.roleCode) {
//       realRoutes.push(v)
//     }
//     arr1.forEach(item => {
//       if (item === v.roleCode) {
//         if (v.children && v.children.length > 0) {
//           v.children = filterRoute(v.children, arr1)
//         }
//         // router.addRoutes(v)
//         realRoutes.push(v)
//       }
//     })
//   })
//   console.log(realRoutes, 'realRoutes')
//   return realRoutes
// }

router.beforeEach((to, from, next) => {
  console.log(router.options.routes, 'over111')
  if (to.path === '/login') {
    next()
  } else {
    const token = sessionStorage.getItem('token')
    if (!token) {
      next('/login')
    } else {
      // 筛选权限
      next()
    }
  }
})
export default router
